package com.structure.project.config;


import com.structure.project.common.bean.Return;
import com.structure.project.common.error.BusinessException;
import com.structure.project.common.error.ServerException;
import com.structure.project.common.error.SystemCodeMsgEnums;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.net.UnknownHostException;
import java.util.stream.Collectors;

/**
 * @author jh
 * @date 2022/12/6 16:07
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionConfig {
    /**
     * 指定异常处理方法
     **/
    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public Return<Object> error(NullPointerException e, HttpServletRequest request) {
        return Return.addResult(SystemCodeMsgEnums.NULL_POINT);
    }

    /**
     * 处理 form data方式调用接口对象参数校验失败抛出的异常
     *
     * @param e       e
     * @param request 请求
     * @return {@link Return }<{@link Object }>
     * @author jh
     * @date 2023/05/12
     */
    @ExceptionHandler(BindException.class)
    @ResponseBody
    public Return<Object> bindExceptionHandler(BindException e, HttpServletRequest request) {
        String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining("; "));
        return Return.addResult(SystemCodeMsgEnums.PARAM_ERROR).message(message);
    }

    /**
     * 处理Get请求中 验证路径中 单个参数请求失败抛出异常
     *
     * @param e       e
     * @param request 请求
     * @return {@link Return }<{@link Object }>
     * @author jh
     * @date 2023/05/12
     */
    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseBody
    public Return<Object> constraintViolationExceptionHandler(ConstraintViolationException e, HttpServletRequest request) {
        String message = e.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.joining("; "));
        return Return.addResult(SystemCodeMsgEnums.PARAM_ERROR).message(message);
    }

    /**
     * 页面不存在异常处理
     *
     * @param httpServletRequest 异常信息
     * @return 错误信息
     */
    @ExceptionHandler(NoHandlerFoundException.class)
    public Return<Object> noHandlerFoundException(HttpServletRequest httpServletRequest) {
        log.error("请求地址错误异常 => 请求方式: {}, 请求地址: {}", httpServletRequest.getMethod(), httpServletRequest.getServletPath());
        return Return.addResult(SystemCodeMsgEnums.NOT_FOUND);
    }

    /**
     * 处理 json 请求体调用接口对象参数校验失败抛出的异常
     *
     * @param e       e
     * @param request 请求
     * @return {@link Return }<{@link Object }>
     * @author jh
     * @date 2023/05/12
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public Return<Object> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e, HttpServletRequest request) {
        String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining("; "));
        return Return.addResult(SystemCodeMsgEnums.PARAM_ERROR).message(message);
    }

    /**
     * 请求方式错误异常处理
     *
     * @param httpServletRequest 异常信息
     * @return 错误信息
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Return<Object> httpRequestMethodNotSupportedException(HttpServletRequest httpServletRequest) {
        log.error("请求方式错误异常 => 请求方式: {}, 请求地址: {}", httpServletRequest.getMethod(), httpServletRequest.getServletPath());
        return Return.addResult(SystemCodeMsgEnums.REQUEST_METHOD_ERROR);
    }

    /**
     * 未知主机异常
     *
     * @param unknownHostException 异常信息
     * @return 错误信息
     */
    @ExceptionHandler(UnknownHostException.class)
    public Return<?> unknownHostException(UnknownHostException unknownHostException) {
        log.error("未知主机异常 => 原因是: {}", unknownHostException.getMessage());
        return Return.addResult(SystemCodeMsgEnums.UNKNOWN);
    }
//    /**
//     * 无访问权限异常处理
//     *
//     * @param accessDeniedException 异常信息
//     * @return 错误信息
//     */
//    @ExceptionHandler(AccessDeniedException.class)
//    public Return<?> accessDeniedException(AccessDeniedException accessDeniedException) {
//        log.error("无访问权限错误 => 原因是: {}", accessDeniedException.getMessage());
//        return Return.addResult(SystemCodeMsgEnums.UNAUTHORIZED);
//    }

    /**
     * 通用异常处理方法
     **/
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Return<Object> error(Exception e, HttpServletRequest request) {
        return Return.addResult(SystemCodeMsgEnums.ERROR);
    }

    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public Return<Object> convertException(BusinessException e, HttpServletRequest request) {
        return Return.failure().code(e.getCode()).message(e.getMessage());
    }

    @ExceptionHandler(ServerException.class)
    @ResponseBody
    public Return<Object> convertException(ServerException e, HttpServletRequest request) {
        return Return.failure().code(e.getCode()).message(e.getMessage());
    }
}
